﻿using ERPU8WebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ERPU8WebApi.Until
{
    //解析U8API接口返回的xml 转化为对应的类
    public class ParsingXML
    {
        //创建查询返回结果类
        public BaseOrderModel CreatOrderModel(string xmlHead, string xmlbody, BaseOrderModelController sooc)
        {
            xmlHead = xmlHead.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
            xmlHead = xmlHead.Split(new string[] { "rs:data>" }, StringSplitOptions.None)[1];
            xmlHead = xmlHead.Split(new string[] { "<z:row" }, StringSplitOptions.None)[1];
            int headlastindex = xmlHead.LastIndexOf("\"");
            xmlHead = xmlHead.Substring(0, headlastindex + 1);

            xmlbody = xmlbody.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
            xmlbody = xmlbody.Split(new string[] { "rs:data>" }, StringSplitOptions.None)[1];
            string[] xmlbodyArr = xmlbody.Split(new string[] { "<z:row" }, StringSplitOptions.None);
            for (int i = 1; i < xmlbodyArr.Length; i++)
            {
                int lastindex = xmlbodyArr[i].LastIndexOf("\"");
                xmlbodyArr[i] = xmlbodyArr[i].Substring(0, lastindex + 1);
            }
            List<string> xmlbodyList = xmlbodyArr.ToList();
            xmlbodyList.RemoveAt(0);
            return sooc.GetBaseOrderModel(xmlHead, xmlbodyList, xmlbodyList.Count);
        }

        //public BaseOrderModel CreatOrderModel(string xmlHead, string xmlbody, BaseOrderModelController sooc, BaseOrderModel soo,
        //    BaseOrderModelClass head, List<BaseOrderModelClass> body)
        //{
        //    xmlHead = xmlHead.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
        //    xmlHead = xmlHead.Split(new string[] { "rs:data>" }, StringSplitOptions.None)[1];
        //    xmlHead = xmlHead.Split(new string[] { "<z:row" }, StringSplitOptions.None)[1];
        //    int headlastindex = xmlHead.LastIndexOf("\"");
        //    xmlHead = xmlHead.Substring(0, headlastindex + 1);

        //    xmlbody = xmlbody.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
        //    xmlbody = xmlbody.Split(new string[] { "rs:data>" }, StringSplitOptions.None)[1];
        //    string[] xmlbodyArr = xmlbody.Split(new string[] { "<z:row" }, StringSplitOptions.None);
        //    for (int i = 1; i < xmlbodyArr.Length; i++)
        //    {
        //        int lastindex = xmlbodyArr[i].LastIndexOf("\"");
        //        xmlbodyArr[i] = xmlbodyArr[i].Substring(0, lastindex + 1);
        //    }
        //    List<string> xmlbodyList = xmlbodyArr.ToList();
        //    xmlbodyList.RemoveAt(0);
        //    return sooc.GetBaseOrderModel(xmlHead, xmlbodyList, xmlbodyList.Count,soo,head,body);
        //}

        //将list<子类>转化为 List<父类>
        public  List<T> PraseList<T>(object listObj)
        {
            List<T> result = new List<T>();
            if (!listObj.GetType().IsGenericType)
                return null;
            if (listObj as System.Collections.ICollection != null)
            {
                var list = (System.Collections.ICollection)listObj;
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        result.Add((T)item);
                    }
                }
            }
            return result;
        }
    }
}